task PointExtend(){
let ExtendPoint = [500,1500,2750,4500,6000,8000,9999,3333,6666,9999];//GNXeh̓_Kv̗
let ExtendCount = 0;				//GNXeh񐔂Lϐ
let Point = GetPoint;				//Point̒li[ϐ

loop{
Point = GetPoint;				//擾𖈃t[mF
SetNormPoint(ExtendPoint[ExtendCount]);		//̃GNXeh̐𖈃t[\
if(Point >= ExtendPoint[ExtendCount]){		//GNXeh̒l𒴂
	ExtendPlayer(1);			//c@{P
	PlaySE("se1UP.wav");			//GNXeh炷
	if(ExtendPoint[ExtendCount] == 9999){
	AddPoint(-9999);			//9999z9999炷
	}
	if(ExtendCount == 9){ExtendCount = 6;}	//GNXeh[v
	ExtendCount++;				//GNXeh񐔁{P
	}
yield;

}
}

task ScoreExtend(){
let ExtendCount = 0;
let Score = GetScore;

loop{
if(ExtendCount == 0){if(Score >= 25000000){
		ExtendPlayer(1);
		ExtendCount = 1;}
	yield;
	}
else if(ExtendCount == 1){if(Score >= 50000000){
		ExtendPlayer(1);
		ExtendCount = 2;}
	yield;
	}
else if(ExtendCount == 2){if(Score >= 80000000){
		ExtendPlayer(1);
		ExtendCount = 3;}
	yield;
	}
else if(ExtendCount == 3){if(Score >= 120000000){
		ExtendPlayer(1);
		ExtendCount = 4;}
	yield;
	}
else if(ExtendCount == 4){if(Score >= 160000000){
		ExtendPlayer(1);
		ExtendCount = 5;}
	yield;
	}
else if(ExtendCount == 5){if(Score >= 200000000){
		ExtendPlayer(1);
		ExtendCount = 6;}
	yield;
	}
else if(ExtendCount == 6){yield;}
}
}

task MagicPointOnStage(){

#include_function ".\Config.txt"

SetCommonData("MAGICPOINT",0);
SetCommonData("DRAWBONUS",0);
yield;
let E = GetCommonData("EASY_START");
let N = GetCommonData("NORMAL_START");
let H = GetCommonData("HARD_START");
let L = GetCommonData("LUNATIC_START");

let AddToEnemy;
let ReduceToLive;
let ReduceToMissRate = [0,0.1,0.4,0.9,1.6,2.5,3.6,5];
let Level;
let Difficultly;
let Graze = GetGraze;
let GrazeA = 0;
let Point = GetPoint;
let PointA = GetPoint;

let FirstPlayer = ChoosePlayerLife() - 4;
let ReduceToFirstPlayer = [1.5,3,5,7,10];
let ReduceRateUp = 1;
let Count = 0;

if(E == 1||E == 2){SetCommonData("MAGICPOINT",5);AddToEnemy = 0.8;ReduceToLive = 0.008333;Level = 10;Difficultly = 0;}
if(N == 1||N == 2){SetCommonData("MAGICPOINT",10);AddToEnemy = 1.4;ReduceToLive = 0.009527;Level = 12;Difficultly = 1;}
if(H == 1||H == 2){SetCommonData("MAGICPOINT",15);AddToEnemy = 2.0;ReduceToLive = 0.011115;Level = 16;Difficultly = 2;}
if(L == 1||L == 2){SetCommonData("MAGICPOINT",25);AddToEnemy = 3.0;ReduceToLive = 0.016667;Level = 20;Difficultly = 3;}
if(E > 2){SetCommonData("MAGICPOINT",25);AddToEnemy = 0.8;ReduceToLive = 0.008333;Level = 10;Difficultly = 0;}
if(N > 2){SetCommonData("MAGICPOINT",50);AddToEnemy = 1.4;ReduceToLive = 0.009527;Level = 12;Difficultly = 1;}
if(H > 2){SetCommonData("MAGICPOINT",75);AddToEnemy = 2.0;ReduceToLive = 0.011115;Level = 16;Difficultly = 2;}
if(L > 2){SetCommonData("MAGICPOINT",125);AddToEnemy = 3.0;ReduceToLive = 0.016667;Level = 20;Difficultly = 3;}

let bonusrate = 1 / (10 * (Difficultly  + 1));

SetCommonData("MAGICPOINT_ADD",AddToEnemy);
SetCommonData("MAGICPOINT_REDUCE",ReduceToLive);
SetCommonData("MAGICPOINT_BONUSRATE",bonusrate);

if(FirstPlayer >= 0){ReduceRateUp = ReduceToFirstPlayer[FirstPlayer];}

loop{
	Count++;
	let a	= GetCommonData("MAGICPOINT");
	let zanki = GetPlayerLife;
	if(zanki >= 8){zanki = 7;}
	let ReduceToMiss;
	Graze = GetGraze;

	if(Count >= 90){SetCommonData("BONUSSCORE",0);
	SetCommonData("DRAWBONUS",0);}

	if(Graze != GrazeA){
	a += (Difficultly  + 1) * zanki * 0.035;
	GrazeA = Graze;
	SetCommonData("MAGICPOINT",a);
	}

	if(OnBomb == true){
	PointA = GetPoint;
	let b;
	let PMS = GetPlayerMoveState();


	if(PMS == MOVE_NORMAL){
	while(OnBomb == true){yield;}
	b = a * 0.7;
	Point = GetPoint;
	let Bonus = trunc((Point - PointA) * 300 * a);

	AddScore(Bonus);
	if(Bonus != 0){
	SetCommonData("BONUSSCORE",Bonus);
	yield;
	SetCommonData("DRAWBONUS",1);
	Count = 0;
	b += Bonus / 2000000;
	}
	}

	else if(PMS == MOVE_SLOW){
	while(OnBomb == true){yield;}
	b = a * 0.95;
	Point = GetPoint;
	let BonusP = trunc((Point - PointA) * (0.4375 - Difficultly * 0.0625));

	CreateEnemyFromFile(GetCurrentScriptDirectory~"\Stage\ZAKO\Enemy00.txt",GetCenterX,GetCenterY,0,0,BonusP + 1);

	SetCommonData("BONUSSCORE",BonusP+1);
	loop(2){yield;}
	SetCommonData("DRAWBONUS",2);
	Count = 0;
	CollectItems;
	}

	SetCommonData("MAGICPOINT",b);
	}

	if(OnPlayerMissed == true){
	while(OnPlayerMissed == true){yield;}//炢{ԑҋ@
	if(OnBomb == false){//炢{ɎsĂ猸
	ReduceToMiss = Level * ReduceToMissRate[zanki] * ReduceRateUp;
	a -= ReduceToMiss;
	if(0 >= a){a = 0;}//MasicPointOȉɂȂOɂ
	SetCommonData("MAGICPOINT",a);
	}
	}

	if(Difficultly <= 1){
	if(a > 999.999999){
	a = 999.999999;
	SetCommonData("MAGICPOINT",a);
	}
	}
	if(Difficultly == 2){
	if(a > 1499.999999){
	a = 1499.999999;
	SetCommonData("MAGICPOINT",a);
	}
	}
	if(Difficultly == 3){
	if(a > 1999.999999){
	a = 1999.999999;
	SetCommonData("MAGICPOINT",a);
	}
	}
yield;
}
}
function NumToString(let num){
let leng = trunc(log10(num))+1;//̌߂
let string = ToString(num);//𕶎ɕς
let lengA = 0;

if(leng < 2&& leng >= 1){lengA = 1;}
if(leng < 3&& leng >= 2){lengA = 2;}
if(leng < 4&& leng >= 3){lengA = 3;}
if(leng < 5&& leng >= 4){lengA = 4;}
if(leng < 6&& leng >= 5){lengA = 5;}
if(leng < 7&& leng >= 6){lengA = 6;}
if(leng < 8&& leng >= 7){lengA = 7;}
if(leng < 9&& leng >= 8){lengA = 8;}
if(leng < 10&& leng >= 9){lengA = 9;}
if(leng < 11&& leng >= 10){lengA = 10;}

return string[0..lengA];//XCVOĕԂ
}

function MagicPointDrawText(){

let textA = "MagicPoint";
let textB = ToString(GetCommonData("MAGICPOINT"));
let textD = "MP"~ToString(GetCommonData("MAGICPOWER"));

let textE = "ScoreBonus!"~NumToString(GetCommonData("BONUSSCORE"));
let textF = "PointItemBonus!"~NumToString(GetCommonData("BONUSSCORE"));
let a = GetCommonDataDefault("ALPHA",255);

if(GetPlayerX < 150&&GetPlayerY > 350){
a -= 16;
if(a < 70){a = 70;}
}
else{
a += 16;
if(a > 255){a = 255;}
}
SetFontColor(190,205,255,190,155,255);
DrawText(textA,64,388,12,a);
DrawText(textB,64,400,12,a);
DrawText(textD,64,424,12,a);
if(GetCommonData("DRAWBONUS") == 1){DrawText(textE,64,48,24,255);}
if(GetCommonData("DRAWBONUS") == 2){DrawText(textF,64,48,24,255);}
SetCommonData("ALPHA",a);
}

task MagicPowerOnStage{
SetCommonData("MAGICPOWER",0);
let PointA = 0;		//Ot[̓_ACe̐i[ϐ
let Point = GetPoint;	//_ACe̐i[ϐ
let MagicPower;		//͒li[ϐ
let MagicPoint;		//͓_i[ϐ
let ExtendCount = 0;	//{GNXehi[ϐ
let E = GetCommonData("EASY_START");
let N = GetCommonData("NORMAL_START");
let H = GetCommonData("HARD_START");
let L = GetCommonData("LUNATIC_START");
let level;
if(E > 0){level = 1;}
if(N > 0){level = 1;}
if(H > 0){level = 1.5;}
if(L > 0){level = 2;}


loop{
MagicPower = GetCommonData("MAGICPOWER");
MagicPoint = GetCommonData("MAGICPOINT");
Point = GetPoint;
//t[A_ACe̐A͒lA͓_擾

if(Point != PointA){					//_ACe擾Ƃ̏
if(Point >= PointA){					//_ACeĂ
MagicPower += MagicPoint * (Point - PointA) / (5000 + ExtendCount * 1000) / level;//MagicPower𑝂₷
SetCommonData("MAGICPOWER",MagicPower);			//ʃf[^XV
}
PointA = Point;						//PointAVlɒ`Ȃ
}

if(MagicPower >= 100){					//POOz
AddBomb(1);						//{{P
PlaySE(GetCurrentScriptDirectory~"\se\tm2_power000.wav");//炷
MagicPower -= 100;					//͒l|POO
ExtendCount++;						//GNXeh񐔁{P
SetCommonData("MAGICPOWER",MagicPower);			//ʃf[^XV
}

yield;	//厖
}
}

function MagicPowerAdd(isBig){
let MP = GetCommonData("MAGICPOWER");
if(isBig == true){
MP += 3;
}
else if(isBig == false){
MP += 0.5;
}
SetCommonData("MAGICPOWER",MP);
}

function MasicPointAddNormal(Rate){
let MagicPoint = GetCommonData("MAGICPOINT");
let AddPoint = GetCommonData("MAGICPOINT_ADD");

if(OnBomb == false){
MagicPoint += AddPoint * Rate;
SetCommonData("MAGICPOINT",MagicPoint);
}
}

function MasicPointAddSpellCard{
let MagicPoint = GetCommonData("MAGICPOINT");
let AddPoint = GetCommonData("MAGICPOINT_ADD");

if(GetBombCountInThisSpell == 0&&GetMissCountInThisSpell == 0){
MagicPoint += AddPoint * 15;
SetCommonData("MAGICPOINT",MagicPoint);
}
}

function MasicPointReduce{
let MagicPoint = GetCommonData("MAGICPOINT");
let ReducePoint = GetCommonData("MAGICPOINT_REDUCE");

MagicPoint -= ReducePoint;
if(0 >= MagicPoint){MagicPoint = 0;}
SetCommonData("MAGICPOINT",MagicPoint);
}

task MusicControl{
let stage;							//Xe[Wi[ϐ
let Music1 = GetCurrentScriptDirectory~"\Bgm\ametowa.mp3";	//Xe[WPy
let Music2 = GetCurrentScriptDirectory~"\Bgm\fukaki.mp3";	//Xe[WQy
let Music3 = GetCurrentScriptDirectory~"\Bgm\toki.mid";		//Xe[WRy

LoadMusic(Music1);
stage = GetCommonData("STAGE");					//ʃf[^ŃXe[WmF
if(stage == 1){PlayMusic(Music1);}				//PʂȂAPʉy炷
while(stage == 1){stage = GetCommonData("STAGE");yield;}	//PʂI܂őҋ@
DeleteMusic(Music1);						//Pʉy폜
LoadMusic(Music2);
if(stage == 2){PlayMusic(Music2);}				//QʂȂAQʉy炷
while(stage == 2){stage = GetCommonData("STAGE");yield;}	//QʂI܂őҋ@
DeleteMusic(Music2);						//Qʉy폜
LoadMusic(Music3);
if(stage == 3){PlayMusic(Music3);}				//RʂȂARʉy炷
while(stage == 3){stage = GetCommonData("STAGE");yield;}	//RʂI܂őҋ@
DeleteMusic(Music3);						//Rʉy폜
loop{yield;}
}

task BGControl{
SetCommonData("ON_SPELL",0);
let stage;
let spell;
let BG1 = GetCurrentScriptDirectory~"\BG\yun_3780.jpg";
let BG2 = GetCurrentScriptDirectory~"\BG\eyes0048.jpg";
let BG3 = GetCurrentScriptDirectory~"\BG\seiryu808.jpg";

stage = GetCommonData("STAGE");

LoadGraphic(BG1);
if(stage == 1){
SetTexture(BG1);SetGraphicRect(0,0,384,448);SetAlpha(150);yield;}
while(stage == 1){stage = GetCommonData("STAGE");yield;}
DeleteGraphic(BG1);

LoadGraphic(BG2);SetGraphicScale(2,2);
if(stage == 2){
SetTexture(BG2);SetGraphicRect(0,0,384,448);SetAlpha(150);yield;}
while(stage == 2){stage = GetCommonData("STAGE");yield;}
DeleteGraphic(BG2);

LoadGraphic(BG3);SetGraphicScale(1,1);
if(stage == 3){
SetTexture(BG3);SetGraphicRect(0,0,384,448);SetAlpha(200);yield;}
while(stage == 3){stage = GetCommonData("STAGE");
spell = GetCommonData("ON_SPELL");SetAlpha(200);
while(spell == 1){SetAlpha(150);yield;}
yield;}
DeleteGraphic(BG3);

loop{yield;}
}
